package com.onluy.sysman.controller.sysmgr;


import com.alibaba.fastjson.JSON;
import com.onluy.frameworkcloud.Param.ParamPagination;
import com.onluy.frameworkcloud.Param.ReqHead;
import com.onluy.frameworkcloud.Param.ReqInfo;
import com.onluy.frameworkcloud.Param.RspInfo;
import com.onluy.frameworkcloud.base.AppWalkController;
import com.onluy.frameworkcloud.data.bean.PageData;
import com.onluy.frameworkcloud.data.util.InParam;
import com.onluy.sysman.param.MenuParam;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;

import com.onluy.sysman.service.TdSysMenusService;
import com.onluy.sysman.entity.sysmgr.TdSysMenus;

@Validated
@RestController
@RequestMapping("/tdSysMenus")
@Api(value="tdSysMenus", description="/TdSysMenus管理接口", position=1 )
public class TdSysMenusController extends AppWalkController {


	//调用服务
	@Autowired
	private TdSysMenusService tdSysMenusService;

	@RequestMapping(value={"/target"},method={ RequestMethod.POST})
	@ApiOperation(value="目标", notes="根据 areaCode,sysCode,menuCode  获取信息")
	public RspInfo<TdSysMenus> dotarget(@Valid @RequestBody ReqInfo<ReqHead, MenuParam> reqInfo   ) throws Exception {
		RspInfo<TdSysMenus> jsonResult = new RspInfo<TdSysMenus>();
		log.info("------------>Model reqInfo : " + JSON.toJSONString(reqInfo));
		TdSysMenus tdSysMenus = new TdSysMenus();

		tdSysMenus.setAreaCode(reqInfo.getReqBody().getAreaCode()).asCondition();
		tdSysMenus.setSysCode(reqInfo.getReqBody().getSysCode()).asCondition();
		tdSysMenus.setMenuCode(reqInfo.getReqBody().getMenuCode()).asCondition();

		tdSysMenus = tdSysMenusService.model(tdSysMenus);
		jsonResult.setRspCode(0);
		jsonResult.setRspData(tdSysMenus);
		return jsonResult;
	}



	@RequestMapping(value={ "/add"},method={RequestMethod.POST })
	@ApiOperation(value="新增", notes="新增")
	public RspInfo<InParam> doAdd(@RequestBody @Valid ReqInfo<ReqHead,TdSysMenus> reqInfo )  throws Exception{
		RspInfo<InParam> jsonResult = new RspInfo<InParam>();
		int num = 0;
		TdSysMenus tdSysMenus = reqInfo.getReqBody();
		num = tdSysMenusService.add(tdSysMenus);
		if (num > 0) {
			jsonResult.setRspCode(0);
			jsonResult.setRspDesc("增加成功！");
		} else {
			jsonResult.setRspDesc("增加失败！");
		}
		return jsonResult;
	}

	@RequestMapping(value={ "/update"},method={RequestMethod.POST})
	@ApiOperation(value="更新", notes="根据 userId 更新")
	public RspInfo<InParam> doUpdate(      @RequestBody @Valid  ReqInfo<ReqHead,TdSysMenus> reqInfo ) throws Exception {
		RspInfo<InParam> jsonResult=new RspInfo<InParam>();
		int num=0;


		TdSysMenus tdSysMenus = reqInfo.getReqBody();

		tdSysMenus.setAreaCode(reqInfo.getReqBody().getAreaCode()).asCondition();
		tdSysMenus.setSysCode(reqInfo.getReqBody().getSysCode()).asCondition();
		tdSysMenus.setMenuCode(reqInfo.getReqBody().getMenuCode()).asCondition();

		num=tdSysMenusService.update( tdSysMenus);
		if(  num>0 ){
			jsonResult.setRspCode(0);
			jsonResult.setRspDesc("修改成功！");
		}
		else {
			jsonResult.setRspDesc("修改失败！");
		}
		return jsonResult;
	}




	@RequestMapping(value={"/del"},method={RequestMethod.POST})
	@ApiOperation(value="删除", notes="根据  id  删除")
	public RspInfo<InParam> doDel(@Valid @RequestBody    ReqInfo<ReqHead,MenuParam> reqInfo) throws Exception {
		RspInfo<InParam> jsonResult=new RspInfo<InParam>();
		TdSysMenus tdSysMenus = new TdSysMenus();
		int num=1;

			tdSysMenus.setAreaCode(reqInfo.getReqBody().getAreaCode()).asCondition();
			tdSysMenus.setSysCode(reqInfo.getReqBody().getSysCode()).asCondition();
			tdSysMenus.setMenuCode(reqInfo.getReqBody().getMenuCode()).asCondition();

			num=tdSysMenusService.del( tdSysMenus);

		if(  num>0 ){
			jsonResult.setRspCode(0);
			jsonResult.setRspDesc("删除成功！");
		}
		else {
			jsonResult.setRspDesc("删除失败！");
		}
		return jsonResult;
	}


	@ResponseBody
	@RequestMapping(value={ "/list"},method={RequestMethod.POST })
	@ApiOperation(value="列表", notes="列表明细")
	public RspInfo<PageData<TdSysMenus>>  doList(@RequestBody    ReqInfo<ParamPagination, TdSysMenus> reqInfo )throws Exception {
		RspInfo<PageData<TdSysMenus>> jsonResult=new RspInfo<PageData<TdSysMenus>>();

		this.MakeLikeEntity(  reqInfo.getReqBody());//处理查询条件

		PageData<TdSysMenus> list =tdSysMenusService.list( reqInfo.getReqBody(),FormatPagination(reqInfo.getReqHead()));
		jsonResult.setRspCode(0);
		jsonResult.setRspData(list);
		return jsonResult;
	}

	public  void  MakeLikeEntity(TdSysMenus tdSysMenus){

		if(tdSysMenus == null )return;


		if(null!=tdSysMenus.getAreaCode()){
			tdSysMenus.setAreaCode(tdSysMenus.getAreaCode()).asCondition();
		}if(null!=tdSysMenus.getSysCode()){
			tdSysMenus.setSysCode(tdSysMenus.getSysCode()).asCondition();
		}if(null!=tdSysMenus.getMenuCode()){
			tdSysMenus.setMenuCode(tdSysMenus.getMenuCode()).asCondition();
		}
		if(null!=tdSysMenus.getParentMenuCode()){
			tdSysMenus.setParentMenuCode(tdSysMenus.getParentMenuCode()).asCondition();
		}
		if(null!=tdSysMenus.getMenuType()){
			tdSysMenus.setMenuType(tdSysMenus.getMenuType()).asCondition();
		}
		if(null!=tdSysMenus.getMenuName()){
			tdSysMenus.setMenuName(null).getCondition().andLike( "%"+tdSysMenus.getMenuName()+"%" );
		}
		if(null!=tdSysMenus.getMenuUrl()){
			tdSysMenus.setMenuUrl(null).getCondition().andLike( "%"+tdSysMenus.getMenuUrl()+"%" );
		}
		if(null!=tdSysMenus.getMenuIcon()){
			tdSysMenus.setMenuIcon(null).getCondition().andLike( "%"+tdSysMenus.getMenuIcon()+"%" );
		}

	}


}
